<?php

use think\migration\Migrator;

class UserUser extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */

    protected string $name = 'user';

    public function up(): void
    {
        $table = $this->table($this->name, [
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'comment'     => '用户表',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id']
        ]);
        $table->addColumn('username', 'string', [
            'limit' => 30, 'null' => false, 'comment' => '用户名'
        ])->addColumn('mobile', 'string', [
            'limit' => 15, 'default' => '', 'null' => true, 'comment' => '手机号'
        ])->addColumn('password', 'string', [
            'limit' => 60, 'null' => false, 'comment' => '密码'
        ])->addColumn('nickname', 'string', [
            'limit' => 30, 'default' => '', 'null' => false, 'comment' => '昵称'
        ])->addColumn('avatar', 'string', [
            'limit' => 255, 'default' => '', 'null' => false, 'comment' => '头像'
        ])->addColumn('gender', 'tinyinteger', [
            'limit' => 1, 'default' => 0, 'null' => false, 'comment' => '性别'
        ])->addColumn('score', 'string', [
            'limit' => 10, 'default' => 0, 'null' => false, 'comment' => '积分'
        ])->addColumn('vip', 'tinyinteger', [
            'limit' => 3, 'default' => 0, 'null' => false, 'comment' => 'vip'
        ])->addColumn('vip_expire', 'datetime', [
            'null' => true, 'comment' => 'vip过期时间'
        ])->addColumn('invite_code', 'string', [
            'limit' => 16, 'default' => '', 'null' => false, 'comment' => '邀请码'
        ])->addColumn('create_at', 'datetime', [
            'null' => false, 'comment' => '添加时间'
        ])->addColumn('login_at', 'datetime', [
            'null' => true, 'comment' => '登录时间'
        ])->addColumn('status', 'tinyinteger', [
            'limit' => 1, 'default' => 1, 'null' => false, 'comment' => '状态'
        ])->addIndex(['username'], [
            'name' => 'username', 'unique' => true
        ])->addIndex(['mobile'], [
            'name' => 'mobile'
        ])->save();
    }

    public function down(): void
    {
        // 删除表
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
